package com.meeting.meeting.controller;

import com.github.pagehelper.PageInfo;
import com.meeting.meeting.model.Admin;
import com.meeting.meeting.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * @author 何星霖
 * @date 2019/12/28 20:47
 */
@Controller
public class AdminController {
    @Autowired
    AdminService adminService;
    @RequestMapping("/getAllAdmin")
    public String getAllAdmin(Admin admin, ModelMap model,
                              @RequestParam(required=true,value="pageNum",defaultValue="1") Integer pageNum,
                              @RequestParam(required=true,value="pageSize",defaultValue="10") Integer pageSize){
        model.put("admin",admin);
        PageInfo<Map<String,Object>> pageInfo = adminService.selectAll(pageNum,pageSize,admin);
        List<Map<String,Object>> list=pageInfo.getList();
        long count=pageInfo.getTotal();
        model.put("AdminList",list);
        model.put("pageInfo",pageInfo);
        model.put("count",count);
        return "pages/admin";
    }
    @RequestMapping("/deletadmin")
    public String delete(Integer id,ModelMap model){
        int a=adminService.deleteByPrimaryKey(id);
        if (a>0){
            model.put("type","Admin");
            return "pages/delete";
        }else {
            model.put("tips","过程出现不安定因素，请重试!");
            return "pages/505";
        }
    }
    @RequestMapping("/getadminbyid")
    public String getAdminbyid(@RequestParam(required=true,value="id") Integer id,ModelMap model){
        Admin admin=adminService.selectByPrimaryKey(id);
        model.put("admin",admin);
        return "pages/adminedit";
    }
    @RequestMapping("/editAdmin")
    public String editAdmin(ModelMap model, Admin admin){
        int a=adminService.updateByPrimaryKey(admin);
        if (a>0){
            model.put("type","Admin");
            return "pages/update";
        }else {
            model.put("tips","过程出现不安定因素，请重试!");
            return "505";
        }
    }
    @RequestMapping("/newAdmin")
    public String newAdmin(ModelMap model, Admin admin){
        int a=adminService.insert(admin);
        if (a>0){
            model.put("type","Admin");
            return "pages/new";
        }else {
            model.put("tips","过程出现不安定因素，请重试!");
            return "505";
        }
    }
    @RequestMapping("/tonewAdmin")
    public String tonewAdmin(ModelMap model, Admin admin){
       return "pages/adminnew";
    }
}
